package cn.share.life.common.sensitiveword.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hopefound.common.core.constant.CacheConstants;
import com.hopefound.common.core.constant.Constants;
import com.hopefound.common.core.domain.R;
import com.hopefound.common.core.utils.StringUtils;
import com.hopefound.common.core.web.controller.BaseController;
import com.hopefound.common.core.web.page.TableDataInfo;
import com.hopefound.common.log.annotation.Log;
import com.hopefound.common.log.enums.BusinessType;
import com.hopefound.common.redis.service.RedisService;
import com.hopefound.common.security.annotation.RequiresPermissions;
import com.hopefound.system.domain.SensitiveWords;
import com.hopefound.system.service.SensitiveWordsService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 敏感词(SensitiveWords)表控制层
 *
 * @author cxj
 * @since 2025-02-19 10:59:36
 */
@Slf4j
@Schema(description = "敏感词")
@RestController
@RequestMapping("/sensitiveWords")
public class SensitiveWordsController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private SensitiveWordsService sensitiveWordsService;
    @Autowired
    private RedisService redisService;

    /**
     * 分页查询所有数据
     *
     * @param sensitiveWords 查询实体
     * @return 所有数据
     */
    @RequiresPermissions("system:sensitiveWords:query")
    @ApiOperation("敏感词-分页查询所有数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "1", defaultValue = "1"), @ApiImplicitParam(name = "pageSize", value = "10", defaultValue = "10")})
    @GetMapping("/pageList")
    public TableDataInfo pageList(SensitiveWords sensitiveWords) {
        startPage();
        List<SensitiveWords> linkList = sensitiveWordsService.list(sensitiveWords);
        return getDataTable(linkList);
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */

    @ApiOperation("敏感词-查询出所有的敏感词")
    @GetMapping("/selectList")
    public R<List<String>> selectList() {
        List<SensitiveWords> list = sensitiveWordsService.list();
        List<String> nameList = list.stream().map(SensitiveWords::getName).distinct().collect(Collectors.toList());
        return R.ok(nameList);
    }

    @ApiOperation("敏感词-更新敏感词出现次数")
    @PostMapping("/updateSensitiveWordCount")
    public R updateSensitiveWordCount(@RequestParam("nameList") List<String> nameList) {
        return R.ok(sensitiveWordsService.updateSensitiveWordCount(nameList));
    }

    /**
     * 新增数据
     *
     * @param sensitiveWords 实体对象
     * @return 新增结果
     */
    @RequiresPermissions("system:sensitiveWords:add")
    @ApiOperation("敏感词-新增数据")
    @PostMapping("/insert")
    @Log(title = "敏感词", businessType = BusinessType.INSERT, systemCode = Constants.BUSINESS_PLATFORM)
    public R insert(@RequestBody SensitiveWords sensitiveWords) {
        long count = this.sensitiveWordsService.count(new LambdaQueryWrapper<SensitiveWords>().eq(SensitiveWords::getName, sensitiveWords.getName()));
        if (count > 0) {
            return R.fail("敏感词已存在");
        }
        boolean saveFlag = this.sensitiveWordsService.save(sensitiveWords);
        if (saveFlag) {
            List<SensitiveWords> list = sensitiveWordsService.list();
            List<String> nameList = list.stream().map(SensitiveWords::getName).distinct().collect(Collectors.toList());
            redisService.setCacheObject(CacheConstants.SENSITIVE_WORD_LIST, nameList);
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 修改数据
     *
     * @param sensitiveWords 实体对象
     * @return 修改结果
     */

    @RequiresPermissions("system:sensitiveWords:edit")
    @ApiOperation("敏感词-修改数据")
    @PostMapping("/update")
    @Log(title = "敏感词", businessType = BusinessType.UPDATE, systemCode = Constants.BUSINESS_PLATFORM)
    public R update(@RequestBody SensitiveWords sensitiveWords) {
        LambdaQueryWrapper<SensitiveWords> wrapper = new LambdaQueryWrapper<SensitiveWords>()
                .eq(SensitiveWords::getName, sensitiveWords.getName())
                .ne(SensitiveWords::getId, sensitiveWords.getId());
        long count = this.sensitiveWordsService.count(wrapper);
        if (count > 0) {
            return R.fail("敏感词已存在");
        }
        SensitiveWords oldSensitiveWord = this.sensitiveWordsService.getById(sensitiveWords.getId());
        if (!StringUtils.equals(oldSensitiveWord.getName(), sensitiveWords.getName())) {
            sensitiveWords.setNum(0L);
        }
        boolean updateFlag = this.sensitiveWordsService.updateById(sensitiveWords);
        if (updateFlag) {
            List<SensitiveWords> list = sensitiveWordsService.list();
            List<String> nameList = list.stream().map(SensitiveWords::getName).distinct().collect(Collectors.toList());
            redisService.setCacheObject(CacheConstants.SENSITIVE_WORD_LIST, nameList);
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @RequiresPermissions("system:sensitiveWords:remove")
    @ApiOperation("敏感词-删除数据")
    @DeleteMapping
    @Log(title = "敏感词", businessType = BusinessType.DELETE, systemCode = Constants.BUSINESS_PLATFORM)
    public R delete(@RequestParam("idList") List<String> idList) {
        boolean deleteFlag = this.sensitiveWordsService.removeByIds(idList);
        if (deleteFlag) {
            List<SensitiveWords> list = sensitiveWordsService.list();
            List<String> nameList = list.stream().map(SensitiveWords::getName).distinct().collect(Collectors.toList());
            redisService.setCacheObject(CacheConstants.SENSITIVE_WORD_LIST, nameList);
            return R.ok();
        }
        return R.fail();
    }

}
